home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / TextComponent.java < prev    next >
Text File  |  1998-09-22  |  16KB  |  485 lines

  1. /*
  2.  * @(#)TextComponent.java    1.34 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package java.awt;
  15.  
  16. import java.awt.peer.TextComponentPeer;
  17. import java.awt.event.*;
  18. import java.io.ObjectOutputStream;
  19. import java.io.ObjectInputStream;
  20. import java.io.IOException;
  21. import sun.awt.SunToolkit;
  22.  
  23.  
  24. /**
  25.  * The <code>TextComponent</code> class is the superclass of 
  26.  * any component that allows the editing of some text. 
  27.  * <p>
  28.  * A text component embodies a string of text.  The 
  29.  * <code>TextComponent</code> class defines a set of methods 
  30.  * that determine whether or not this text is editable. If the
  31.  * component is editable, it defines another set of methods
  32.  * that supports a text insertion caret. 
  33.  * <p>
  34.  * In addition, the class defines methods that are used 
  35.  * to maintain a current <em>selection</em> from the text. 
  36.  * The text selection, a substring of the component's text, 
  37.  * is the target of editing operations. It is also referred
  38.  * to as the <em>selected text</em>.
  39.  *
  40.  * @version    1.34, 07/01/98
  41.  * @author     Sami Shaio
  42.  * @author     Arthur van Hoff
  43.  * @since       JDK1.0
  44.  */
  45. public class TextComponent extends Component {
  46.  
  47.     /**
  48.      * The value of the text.
  49.      */
  50.     String text;
  51.  
  52.     /**
  53.      * A boolean indicating whether or not this TextComponent is editable.
  54.      */
  55.     boolean editable = true;
  56.  
  57.     /**
  58.      * The selection start.
  59.      */
  60.     int selectionStart;
  61.  
  62.     /**
  63.      * The selection end.
  64.      */
  65.     int selectionEnd;
  66.  
  67.     transient protected TextListener textListener;
  68.  
  69.     /*
  70.      * JDK 1.1 serialVersionUID 
  71.      */
  72.     private static final long serialVersionUID = -2214773872412987419L;
  73.  
  74.     /**
  75.      * Constructs a new text component initialized with the 
  76.      * specified text. Sets the value of the cursor to 
  77.      * <code>Cursor.TEXT_CURSOR</code>.
  78.      * @param      text the initial text that the component presents.
  79.      * @see        java.awt.Cursor
  80.      * @since      JDK1.0
  81.      */
  82.     TextComponent(String text) {
  83.     this.text = text;
  84.     setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
  85.     }
  86.  
  87.     boolean areInputMethodsEnabled() {
  88.     try {
  89.             Toolkit toolkit = Toolkit.getDefaultToolkit();
  90.             return ((SunToolkit) toolkit).enableInputMethodsForTextComponent();
  91.         } catch (Exception e) {
  92.             // if something bad happens, just don't enable input methods
  93.             return false;
  94.     }
  95.     }
  96.  
  97.     /**
  98.      * Removes the TextComponent's peer.  The peer allows us to modify
  99.      * the appearance of the TextComponent without changing its
  100.      * functionality.
  101.      */
  102.     public void removeNotify() {
  103.       synchronized(getTreeLock()) {
  104.     TextComponentPeer peer = (TextComponentPeer)this.peer;
  105.     if (peer != null) {
  106.         text = peer.getText();
  107.         selectionStart = peer.getSelectionStart();
  108.         selectionEnd = peer.getSelectionEnd();
  109.     }
  110.     super.removeNotify();
  111.       }
  112.     }
  113.  
  114.     /**
  115.      * Sets the text that is presented by this 
  116.      * text component to be the specified text. 
  117.      * @param       t   the new text.
  118.      * @see         java.awt.TextComponent#getText  
  119.      * @since       JDK1.0
  120.      */
  121.     public synchronized void setText(String t) {
  122.     text = t;
  123.     TextComponentPeer peer = (TextComponentPeer)this.peer;
  124.     if (peer != null) {
  125.         peer.setText(t);
  126.     }
  127.     }
  128.  
  129.     /**
  130.      * Gets the text that is presented by this text component.
  131.      * @see     java.awt.TextComponent#setText
  132.      * @since   JDK1.0
  133.      */
  134.     public synchronized String getText() {
  135.     TextComponentPeer peer = (TextComponentPeer)this.peer;
  136.     if (peer != null) {
  137.         text = peer.getText();
  138.     }
  139.     return text;
  140.     }
  141.  
  142.     /**
  143.      * Gets the selected text from the text that is
  144.      * presented by this text component.  
  145.      * @return      the selected text of this text component.
  146.      * @see         java.awt.TextComponent#select
  147.      * @since       JDK1.0
  148.      */
  149.     public synchronized String getSelectedText() {
  150.     return getText().substring(getSelectionStart(), getSelectionEnd());
  151.     }
  152.  
  153.     /**
  154.      * Indicates whether or not this text component is editable.
  155.      * @return     <code>true</code> if this text component is
  156.      *                  editable; <code>false</code> otherwise.
  157.      * @see        java.awt.TextComponent#setEditable
  158.      * @since      JDK1ble
  159.      */
  160.     public boolean isEditable() {
  161.     return editable;
  162.     }
  163.  
  164.     /**
  165.      * Sets the flag that determines whether or not this
  166.      * text component is editable.
  167.      * <p>
  168.      * If the flag is set to <code>true</code>, this text component 
  169.      * becomes user editable. If the flag is set to <code>false</code>, 
  170.      * the user cannot change the text of this text component. 
  171.      * @param     t   a flag indicating whether this text component 
  172.      *                      should be user editable.
  173.      * @see       java.awt.TextComponent#isEditable
  174.      * @since     JDK1.0
  175.      */
  176.     public synchronized void setEditable(boolean b) {
  177.     editable = b;
  178.     TextComponentPeer peer = (TextComponentPeer)this.peer;
  179.     if (peer != null) {
  180.         peer.setEditable(b);
  181.     }
  182.     }
  183.  
  184.     /**
  185.      * Gets the start position of the selected text in 
  186.      * this text component. 
  187.      * @return      the start position of the selected text. 
  188.      * @see         java.awt.TextComponent#setSelectionStart
  189.      * @see         java.awt.TextComponent#getSelectionEnd
  190.      * @since       JDK1.0
  191.      */
  192.     public synchronized int getSelectionStart() {
  193.     TextComponentPeer peer = (TextComponentPeer)this.peer;
  194.     if (peer != null) {
  195.         selectionStart = peer.getSelectionStart();
  196.     }
  197.     return selectionStart;
  198.     }
  199.  
  200.     /**
  201.      * Sets the selection start for this text component to  
  202.      * the specified position. The new start point is constrained 
  203.      * to be at or before the current selection end. It also
  204.      * cannot be set to less than zero, the beginning of the 
  205.      * component's text.
  206.      * If the caller supplies a value for <code>selectionStart</code>
  207.      * that is out of bounds, the method enforces these constraints
  208.      * silently, and without failure.
  209.      * @param       selectionStart   the start position of the 
  210.      *                        selected text.
  211.      * @see         java.awt.TextComponent#getSelectionStart
  212.      * @see         java.awt.TextComponent#setSelectionEnd
  213.      * @since       JDK1.1
  214.      */
  215.     public synchronized void setSelectionStart(int selectionStart) {
  216.     /* Route through select method to enforce consistent policy
  217.          * between selectionStart and selectionEnd.
  218.          */
  219.     select(selectionStart, getSelectionEnd());
  220.     }
  221.  
  222.     /**
  223.      * Gets the end position of the selected text in 
  224.      * this text component. 
  225.      * @return      the end position of the selected text. 
  226.      * @see         java.awt.TextComponent#setSelectionEnd
  227.      * @see         java.awt.TextComponent#getSelectionStart
  228.      * @since       JDK1.0
  229.      */
  230.     public synchronized int getSelectionEnd() {
  231.     TextComponentPeer peer = (TextComponentPeer)this.peer;
  232.     if (peer != null) {
  233.         selectionEnd = peer.getSelectionEnd();
  234.     }
  235.     return selectionEnd;
  236.     }
  237.  
  238.     /**
  239.      * Sets the selection end for this text component to  
  240.      * the specified position. The new end point is constrained 
  241.      * to be at or after the current selection start. It also
  242.      * cannot be set beyond the end of the component's text.
  243.      * If the caller supplies a value for <code>selectionEnd</code>
  244.      * that is out of bounds, the method enforces these constraints
  245.      * silently, and without failure.
  246.      * @param       selectionEnd   the end position of the 
  247.      *                        selected text.
  248.      * @see         java.awt.TextComponent#getSelectionEnd
  249.      * @see         java.awt.TextComponent#setSelectionStart
  250.      * @since       JDK1.1
  251.      */
  252.     public synchronized void setSelectionEnd(int selectionEnd) {
  253.     /* Route through select method to enforce consistent policy
  254.          * between selectionStart and selectionEnd.
  255.          */
  256.     select(getSelectionStart(), selectionEnd);
  257.     }
  258.     
  259.     /**
  260.      * Selects the text between the specified start and end positions.
  261.      * <p>
  262.      * This method sets the start and end positions of the 
  263.      * selected text, enforcing the restriction that the end 
  264.      * position must be greater than or equal to the start position.
  265.      * The start position must be greater than zero, and the 
  266.      * end position must be less that or equal to the length
  267.      * of the text component's text. If the caller supplies values
  268.      * that are inconsistent or out of bounds, the method enforces 
  269.      * these constraints silently, and without failure.
  270.      * @param        selectionStart the start position of the 
  271.      *                             text to select.
  272.      * @param        selectionEnd the end position of the 
  273.      *                             text to select.
  274.      * @see          java.awt.TextComponent#setSelectionStart
  275.      * @see          java.awt.TextComponent#setSelectionEnd
  276.      * @see          java.awt.TextComponent#selectAll
  277.      * @since        JDK1.0
  278.      */
  279.     public synchronized void select(int selectionStart, int selectionEnd) {
  280.     String text = getText();
  281.     if (selectionStart < 0) {
  282.         selectionStart = 0;
  283.     }
  284.     if (selectionEnd > text.length()) {
  285.         selectionEnd = text.length();
  286.     }
  287.     if (selectionEnd < selectionStart) {
  288.         selectionEnd = selectionStart;
  289.     }
  290.     if (selectionStart > selectionEnd) {
  291.         selectionStart = selectionEnd;
  292.     }
  293.  
  294.     this.selectionStart = selectionStart;
  295.     this.selectionEnd = selectionEnd;
  296.  
  297.     TextComponentPeer peer = (TextComponentPeer)this.peer;
  298.     if (peer != null) {
  299.         peer.select(selectionStart, selectionEnd);
  300.     }
  301.     }
  302.  
  303.     /**
  304.      * Selects all the text in this text component.
  305.      * @see        java.awt.TextComponent@select
  306.      * @since      JDK1.0
  307.      */
  308.     public synchronized void selectAll() {
  309.     String text = getText();
  310.     this.selectionStart = 0;
  311.     this.selectionEnd = getText().length();
  312.  
  313.     TextComponentPeer peer = (TextComponentPeer)this.peer;
  314.     if (peer != null) {
  315.         peer.select(selectionStart, selectionEnd);
  316.     }
  317.     }
  318.  
  319.     /**
  320.      * Sets the position of the text insertion caret for 
  321.      * this text component.
  322.      * 
  323.      * @param        position the position of the text insertion caret.
  324.      * @exception    IllegalArgumentException if the value supplied
  325.      *                   for <code>position</code> is less than zero.
  326.      * @since        JDK1.1
  327.      */
  328.     public synchronized void setCaretPosition(int position) {
  329.     if (position < 0) {
  330.         throw new IllegalArgumentException("position less than zero.");
  331.     }
  332.  
  333.     int maxposition = getText().length();
  334.     if (position > maxposition) {
  335.         position = maxposition;
  336.     }
  337.  
  338.     TextComponentPeer peer = (TextComponentPeer)this.peer;
  339.     if (peer != null) {
  340.         peer.setCaretPosition(position);
  341.     } else {
  342.         throw new IllegalComponentStateException("Cannot set caret position until after the peer has been created");
  343.     }
  344.     }
  345.  
  346.     /**
  347.      * Gets the position of the text insertion caret for 
  348.      * this text component.
  349.      * @return       the position of the text insertion caret.
  350.      * @since        JDK1.1
  351.      */
  352.     public synchronized int getCaretPosition() {
  353.         TextComponentPeer peer = (TextComponentPeer)this.peer;
  354.     int position = 0;
  355.  
  356.     if (peer != null) {
  357.         position = peer.getCaretPosition();
  358.     } 
  359.     return position;
  360.     }
  361.  
  362.     /**
  363.      * Adds the specified text event listener to recieve text events 
  364.      * from this textcomponent.
  365.      * @param l the text event listener
  366.      */ 
  367.     public synchronized void addTextListener(TextListener l) {
  368.     textListener = AWTEventMulticaster.add(textListener, l);
  369.         newEventsOnly = true;
  370.     }
  371.  
  372.     /**
  373.      * Removes the specified text event listener so that it no longer
  374.      * receives text events from this textcomponent
  375.      */ 
  376.     public synchronized void removeTextListener(TextListener l) {
  377.     textListener = AWTEventMulticaster.remove(textListener, l);
  378.     }
  379.  
  380.     // REMIND: remove when filtering is done at lower level
  381.     boolean eventEnabled(AWTEvent e) {
  382.         if (e.id == TextEvent.TEXT_VALUE_CHANGED) {
  383.             if ((eventMask & AWTEvent.TEXT_EVENT_MASK) != 0 ||
  384.                 textListener != null) {
  385.                 return true;
  386.             } 
  387.             return false;
  388.         }
  389.         return super.eventEnabled(e);
  390.     }     
  391.  
  392.     /**
  393.      * Processes events on this textcomponent. If the event is a
  394.      * TextEvent, it invokes the processTextEvent method,
  395.      * else it invokes its superclass's processEvent.
  396.      * @param e the event
  397.      */
  398.     protected void processEvent(AWTEvent e) {
  399.         if (e instanceof TextEvent) {
  400.             processTextEvent((TextEvent)e);
  401.             return;
  402.         }
  403.     super.processEvent(e);
  404.     }
  405.  
  406.     /** 
  407.      * Processes text events occurring on this text component by
  408.      * dispatching them to any registered TextListener objects.
  409.      * NOTE: This method will not be called unless text events
  410.      * are enabled for this component; this happens when one of the
  411.      * following occurs:
  412.      * a) A TextListener object is registered via addTextListener()
  413.      * b) Text events are enabled via enableEvents()
  414.      * @see Component#enableEvents
  415.      * @param e the text event
  416.      */ 
  417.     protected void processTextEvent(TextEvent e) {
  418.         if (textListener != null) {
  419.             int id = e.getID();
  420.         switch (id) {
  421.         case TextEvent.TEXT_VALUE_CHANGED:
  422.         textListener.textValueChanged(e);
  423.         break;
  424.         }
  425.         }
  426.     }
  427.  
  428.     /**
  429.      * Returns the parameter string representing the state of this text 
  430.      * component. This string is useful for debugging. 
  431.      * @return      the parameter string of this text component.
  432.      * @since       JDK1.0
  433.      */
  434.     protected String paramString() {
  435.     String str = super.paramString() + ",text=" + getText();
  436.     if (editable) {
  437.         str += ",editable";
  438.     }
  439.     return str + ",selection=" + getSelectionStart() + "-" + getSelectionEnd();
  440.     }
  441.  
  442.  
  443.     /* 
  444.      * Serialization support.  Since the value of the fields
  445.      * selectionStart, and selectionEnd, and text aren't neccessarily
  446.      * up to date we sync them up with the peer before serializing.
  447.      */
  448.  
  449.     private int textComponentSerializedDataVersion = 1;
  450.  
  451.  
  452.     private void writeObject(java.io.ObjectOutputStream s)
  453.       throws java.io.IOException 
  454.     {
  455.       TextComponentPeer peer = (TextComponentPeer)this.peer;
  456.       if (peer != null) {
  457.     text = peer.getText();
  458.     selectionStart = peer.getSelectionStart();
  459.     selectionEnd = peer.getSelectionEnd();
  460.       }
  461.       s.defaultWriteObject();
  462.  
  463.       AWTEventMulticaster.save(s, textListenerK, textListener);
  464.       s.writeObject(null);
  465.     }
  466.  
  467.  
  468.     private void readObject(ObjectInputStream s)
  469.       throws ClassNotFoundException, IOException 
  470.     {
  471.       s.defaultReadObject();
  472.  
  473.       Object keyOrNull;
  474.       while(null != (keyOrNull = s.readObject())) {
  475.     String key = ((String)keyOrNull).intern();
  476.  
  477.     if (textListenerK == key) 
  478.       addTextListener((TextListener)(s.readObject()));
  479.  
  480.     else // skip value for unrecognized key
  481.       s.readObject();
  482.       }
  483.     }
  484. }
  485.